﻿/*
 * 文件名称：
 * 程序员：wangyun
 * 创建时间：2008/05/27
 * 
 * 功能说明：功能模块字段的字段类型处理
 * 
 * 修改人员：
 * 
 * 修改日期：
 * 
 * 修改记录：
 */

using System;
using System.Collections;
using System.Data;

public class FieldCommon
{
    /// <summary>
    /// 获取字段属性
    /// </summary>
    /// <param name="showType">字段类型</param>
    /// <returns>字段属性</returns>
    public static Hashtable GetFieldType(FieldType showType)
    {
        Hashtable hashTable = new Hashtable();
        string typeName = "";
        int typeLength = 0;
        bool isAllowNull = true;
        bool isIdentity = false;
        int identityIncrement = 0;
        int identitySeed = 0;
        string defaultValue = "";

        #region
        switch (showType)
        {
            case FieldType.None:
                typeName = "nvarchar";
                typeLength = 256;
                break;
            case FieldType.TextType:
                typeName = "nvarchar";
                typeLength = 256;
                break;
            case FieldType.MultipleTextType:
                typeName = "nvarchar";
                typeLength = 2048;
                break;
            case FieldType.MultipleHtmlTextType:
                typeName = "ntext";
                typeLength = 16;
                break;
            case FieldType.ListBoxType:
                typeName = "nvarchar";
                typeLength = 1024;
                break;
            case FieldType.NumberType:
                typeName = "int";
                typeLength = 4;
                break;
            case FieldType.MoneyType:
                typeName = "money";
                typeLength = 8;
                break;
            case FieldType.DateTimeType:
                typeName = "datetime";
                typeLength = 8;
                break;
            case FieldType.LinkType:
                typeName = "nvarchar";
                typeLength = 1024;
                break;
            case FieldType.BoolType:
                typeName = "nvarchar";
                typeLength = 2;
                break;
            case FieldType.PictureType:
                typeName = "nvarchar";
                typeLength = 256;
                break;
            case FieldType.FileType:
                typeName = "nvarchar";
                typeLength = 256;
                break;
            case FieldType.ColorType:
                typeName = "nvarchar";
                typeLength = 64;
                break;
            case FieldType.AreaType:
                typeName = "nvarchar";
                typeLength = 256;
                break;
            case FieldType.PassWordType:
                typeName = "nvarchar";
                typeLength = 256;
                break;
        }
        #endregion
        hashTable.Add("typeName", typeName);
        hashTable.Add("typeLength", typeLength);
        hashTable.Add("isAllowNull", isAllowNull);
        hashTable.Add("isIdentity", isIdentity);
        hashTable.Add("identityIncrement", identityIncrement);
        hashTable.Add("identitySeed", identitySeed);
        hashTable.Add("defaultValue", defaultValue);
        return hashTable;
    }

    /// <summary>
    /// 更新操作时获取绑定值
    /// </summary>
    /// <param name="fieldID">字段 ID</param>
    /// <param name="itemID">字段主键 ID</param>
    /// <returns>值</returns>
    public static string GetBindValue(int fieldID, int itemID)
    {
        Whir.Model.Whir_Field fieldModel = new Whir.Model.Whir_Field();
        Whir.BLL.Whir_Field fieldBll = new Whir.BLL.Whir_Field();
        fieldModel = fieldBll.GetModel(fieldID);
        string fieldname = fieldModel.fieldName;    //得到字段名称
        string tableName = fieldBll.GetModelTableName(fieldID);    //表名

        DataTable dt = new DataTable();
        string sql = string.Format("SELECT [{0}] FROM {1} WHERE {2} = {3}", fieldname, tableName, ContentManage.GetCustomTablePrimaryKeyName(tableName), itemID);
        object result = Whir.SQLServerDAL.DbHelperSQL.GetSingle(sql);
        return result == null ? "" : ContentManage.htmlDecode(result.ToString());
    }

    /// <summary>
    /// 根据枚举值取得枚举描述
    /// </summary>
    /// <param name="showType">FieldType 枚举</param>
    /// <returns>返回枚举的描述信息</returns>
    public static string GetFieldTypeDesc(FieldType showType)
    {
        string strDesc = "";
        switch (showType)
        {
            case FieldType.BoolType: strDesc = "是/否"; break;
            case FieldType.ColorType: strDesc = "颜色代码"; break;
            case FieldType.DateTimeType: strDesc = "日期和时间"; break;
            case FieldType.FileType: strDesc = "上传文件"; break;
            case FieldType.LinkType: strDesc = "超链接"; break;
            case FieldType.ListBoxType: strDesc = "选项"; break;
            case FieldType.MoneyType: strDesc = "货币"; break;
            case FieldType.MultipleHtmlTextType: strDesc = "文本编辑器"; break;
            case FieldType.MultipleTextType: strDesc = "多行文本框"; break;
            case FieldType.NumberType: strDesc = "数字"; break;
            case FieldType.PictureType: strDesc = "上传图片"; break;
            case FieldType.TextType: strDesc = "单行文本框"; break;
            case FieldType.None: strDesc = "传参"; break;
            case FieldType.AreaType: strDesc = "地区"; break;
            case FieldType.PassWordType: strDesc = "密码型文本框"; break;
            default: strDesc = ""; break;
        }
        return strDesc;
    }

    /// <summary>
    /// 返回输入的字符串是否为sqlserver关键字
    /// </summary>
    public static bool IsSqlKeyWord(System.Web.UI.Page page, string field)
    {
        bool flag = false;
        int counter = 0;
        string line = "";
        System.IO.StreamReader file =
           new System.IO.StreamReader(page.Server.MapPath("~/Install/sqlscript/sqlkeyword.txt"));
        while ((line = file.ReadLine()) != null)
        {
            if (line.ToLower().Equals(field.ToLower()))
            {
                flag = true;
                break;
            }
            counter++;
        }
        file.Close();
        return flag;
    }
}
